﻿using System;
using System.IO;
using System.Net;
using Bridge.Base.Converter;
using Pathfinder.Base.Constructs;
using Pathfinder.Utilities;

namespace Pathfinder.Converters.D20PFSRD
{
    public class PFSRDUriConverter : UriConverter<PathfinderCharacter>
    {

        #region IUriConverter Members

        public override PathfinderCharacter Convert(Uri uri)
        {
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.Timeout = 10000;
            WebResponse webResponse = webRequest.GetResponse();

            string webPage = null;
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                if (streamReader.ReadUntilString("sites-layout-tile sites-tile-name-content-1\">"))
                {
                    webPage = streamReader.StripHTML("<.+?>");
                }
            }

            return new PFSRDStatblockConverter().Convert(webPage);
        }

        public override bool ConvertsFiles()
        {
            return false;
        }

        public override bool ConvertsWebFiles()
        {
            return true;
        }

        public override string GetConverterName()
        {
            return "D20PFSRD.com Webpage Statblock Converter";
        }

        public override string GetConverterDescription()
        {
            return "Converts Statblocks from D20PFSRD.com\n" +
                "Limited functionality of conversion from Paizo PDFs.\n" +
                "Created by Zachare Sylvestre (Imper1um)";
        }

        #endregion
    }
}
